const { Device, Counter, Category, Department, Repair, sequelize } = require('../common/mysql.js')
const { Op } = require('sequelize')

module.exports = {
    async getAllFree(store_id) {
        const freeIdList = await Counter.findAll({
            attributes: ['device_id'],
            where: {
                status: {
                    [Op.eq]: 2,
                },
            },
        })
        return await Device.findAll({
            attributes: {
                exclude: ['store_id'],
            },
            include: [
                {
                    model: Category,
                    attributes: [],
                    where: {
                        type: {
                            [Op.eq]: 'DN-2',
                        },
                    },
                },
            ],
            where: {
                id: {
                    [Op.notIn]: freeIdList.map((item) => item.device_id),
                },
                store_id: {
                    [Op.eq]: store_id,
                },
                name: {
                    [Op.like]: '专柜磅秤%',
                },
            },
        })
    },
    async getOne(id, store_id) {
        return await Device.findOne({
            attributes: {
                exclude: ['id', 'store_id'],
            },
            where: {
                id: {
                    [Op.eq]: id,
                },
                store_id: {
                    [Op.eq]: store_id,
                },
            },
        })
    },
    async getAllRepair(id, store_id) {
        return await Device.findOne({
            attributes: {
                exclude: ['id', 'store_id'],
            },
            include: [
                {
                    model: Category,
                },
                {
                    model: Repair,
                },
                {
                    model: Department,
                },
            ],
            where: {
                id: {
                    [Op.eq]: id,
                },
                store_id: {
                    [Op.eq]: store_id,
                },
            },
        })
    },
    async getAll(store_id, query) {
        const { page, pageSize, serial, name, model, identify, ip, mac, category, department, prop, order } = query
        const start = (page - 1) * pageSize
        const condition = {
            store_id: {
                [Op.eq]: store_id,
            },
        }
        if (serial) {
            condition.serial = {
                [Op.like]: `%${serial}%`,
            }
        }
        if (name) {
            condition.name = {
                [Op.like]: `%${name}%`,
            }
        }
        if (model) {
            condition.model = {
                [Op.like]: `%${model}%`,
            }
        }
        if (identify) {
            condition.identify = {
                [Op.like]: `%${identify}%`,
            }
        }
        if (ip) {
            condition.ip = {
                [Op.like]: `%${ip}%`,
            }
        }
        if (mac) {
            condition.mac = {
                [Op.like]: `%${mac}%`,
            }
        }
        const categoryCondition = {}
        if (category && category.length) {
            categoryCondition.id = {
                [Op.in]: category,
            }
        }
        const departmentCondition = {}
        if (department && department.length) {
            departmentCondition.id = {
                [Op.in]: department,
            }
        }
        const orderByCondition = []
        if (prop && order) {
            if (prop.includes('.')) {
                orderByCondition.push([...prop.split('.'), order])
            } else {
                orderByCondition.push([prop, order])
            }
        }
        return await Device.findAndCountAll({
            include: [
                {
                    model: Category,
                    where: categoryCondition,
                },
                {
                    model: Department,
                    where: departmentCondition,
                },
            ],
            attributes: {
                exclude: ['store_id'],
            },
            where: condition,
            order: orderByCondition,
            offset: start ? start : 0,
            limit: pageSize ? Number(pageSize) : null,
            distinct: true,
        })
    },
    async getAllByStoreId(store_id) {
        return await Device.findAll({
            include: [
                {
                    model: Category,
                },
                {
                    model: Department,
                },
            ],
            attributes: {
                exclude: ['store_id'],
            },
            where: {
                store_id: {
                    [Op.eq]: store_id,
                },
            },
        })
    },
    async insert(device) {
        return await Device.create(device)
    },
    async update(id, store_id, device) {
        await Device.update(device, {
            fields: ['serial', 'model', 'name', 'remark', 'ip', 'mac', 'begin', 'end', 'category_id', 'department_id'],
            where: {
                id: {
                    [Op.eq]: id,
                },
                store_id: {
                    [Op.eq]: store_id,
                },
            },
        })
    },
    async del(id, store_id) {
        await Device.destroy({
            where: {
                id: {
                    [Op.eq]: id,
                },
                store_id: {
                    [Op.eq]: store_id,
                },
            },
        })
    },
    async getCountByCategory() {
        const result = await sequelize.query(`
            SELECT
                COUNT( device.id ) AS value,
                category.type,
                category.name
            FROM
                device
                RIGHT JOIN category ON device.category_id = category.id 
            WHERE
                category.type LIKE 'DN-%' 
            GROUP BY
                category.type`)
        return result[0]
    },
}
